<template>
  <section>
    <div class="img" v-if="pic" ref="pic">
      点击添加图片
      <input type="file" accept="image/*" @change="resizeImage($event)">
    </div>
    <div class="blank" v-else></div>
    <textarea cols="18" rows="2" placeholder="点击这里
定制你的问题" v-model="question.questionName"></textarea>
    <div class="flex-row">
      <div class="pics active" v-for="(pic, index) in question.optionVoList"
      :key="index">
        <div class="options img">
          <input type="file" accept="image/*" @change="addNewImage($event, index)">
          点击添加图片
          <svg v-if="question.optionVoList.length > 2"
          class="icon" aria-hidden="true"
          @click="deleteOne(index)">
            <use xlink:href="#icon-shanchuguanbicha2"></use>
          </svg>
        </div>
      </div>
    </div>
    <div class="addNew" @click="addNew">添加一个新的选项</div>
  </section>
</template>

<script>
import { checkQuestion } from '@/api/common'
import {uploadImage} from '@/api/image'

export default {
  name: 'question',
  data () {
    return {
      question: {
        questionName: '',
        sort: 0,
        type: 6,
        imgUrl: '',
        isSkip: 0,
        optionVoList: [
          '',
          ''
        ]
      }
    }
  },
  props: [
    'pic'
  ],
  methods: {
    resizeImage (e) {
      let file = e.target.files[0]
      window.lrz(file)
      .then(rst => {
        this.$refs.pic.style.backgroundImage = `url(${rst.base64})`
        return rst
      })
      .then(rst => {
        uploadImage(rst.formData).then(rst => {
          let imageUrl = rst.data.data
          this.question.imgUrl = imageUrl
        })
      })
    },
    addNewImage (e, index) {
      let file = e.target.files[0]
      window.lrz(file)
      .then(rst => {
        e.target.parentElement.style.backgroundImage = `url(${rst.base64})`
        //  this.question.optionVoList[index] = `url(${rst.base64})`
        return rst
      })
      .then(rst => {
        uploadImage(rst.formData).then(rst => {
          let imageUrl = rst.data.data
          this.question.optionVoList[index] = imageUrl
        })
      })
    },
    deleteOne (index) {
      if (this.question.optionVoList.length > 2) {
        this.question.optionVoList.splice(index, 1)
        console.log(index)
      }
    },
    addNew () {
      this.question.optionVoList.push('')
    },
    trans () {
      if (checkQuestion(this.question, 6)) {
        this.$emit('addSurvey', this.question)
        return true
      } else {
        return false
      }
    }
  }
}
</script>

<style lang="scss" scoped>
section {
  display: flex;
  flex-direction: column;
  align-items: center;
  background-color: #fff;
}
textarea {
  text-align: center;
  margin: 30px auto;
  font-size: 48px;
  color: #999;
  border: none;
  resize: none;
  &::-webkit-input-placeholder {
    text-align: center;
    color: #999;
  }
}
.img {
  width: 750px;
  height: 750px;
  background-color: #4b539e;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 40px;
  color: #999;
  position: relative;
  background-size: cover;
  input {
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    opacity: 0;
  }
}
.blank {
  width: 100%;
  height: 100px;
}
img {
  height: 100%;
  width: 100%;
}
.flex-row {
  width: 650px;
  display: flex;
  justify-content: space-between;
  flex-wrap: wrap;
}
.pics {
  position: relative;
  width: 300px;
  height: 300px;
  margin-bottom: 50px;
}
.icon {
  position: absolute;
  top: 0;
  right: 0;
  width: 60px;
  height: 60px;
  transform: translate3d(50%, -50%, 0);
  color: #ff4f00;
}
.options {
  position: relative;
  width: 300px;
  height: 300px;
}
.addNew {
  border: 2px dashed #999;
  color: #999;
  width: 400px;
  height: 150px;
  font-size: 40px;
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>

